#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
int main()
{
    int fd = open("log.txt",O_RDONLY);

    dup2(fd,0);//输入重定向

    char buffer[1024];
    size_t s = read(0,buffer,sizeof(buffer));
    if(s > 0)
    {
        buffer[s] = '\0';
        printf("stdin redir:%s\n",buffer);
    }

    //write(1,"hahaha\n",7);
    //printf("hello fd:%d\n",fd);
    //fprintf(stdout,"hello fd:%d\n",fd);
    //fputs("hello\n",stdout);

    //const char* mess = "hello fd\n";
    //fwrite(mess,1, strlen(mess),stdout);

    //close(1); //将stdout关掉
    //int fd1 = open("log1.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);
    //int fd2 = open("log2.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);
    //int fd3 = open("log3.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);
    //int fd4 = open("log4.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);
    //
    //printf("fd1:%d\n",fd1);
    //printf("fd2:%d\n",fd2);
    //printf("fd3:%d\n",fd3);
    //printf("fd4:%d\n",fd4);
    //
    //fflush(stdout);

    //close(fd1);
    //close(fd2);
    //close(fd3);
    //close(fd4);



    //int a = 0;
    ////char buffer[1024] = { 0  };
    ////read(0,buffer,sizeof(buffer));
    //read(0,&a,sizeof(a));
    //printf("%d\n",a);


//    printf("stdin:%d\n",stdin->_fileno);
//    printf("stdout:%d\n",stdout->_fileno);
//    printf("stderr:%d\n",stderr->_fileno);
//    FILE* fd = fopen("log1.txt", "w");
//    printf("fd:%d\n",fd->_fileno);
//
    //只读，不存在，打开失败
    // open("log.txt",O_WRONLY); // C语言只需指定读取方式是因为库已经封装好了！
    
    //只读，指定文件不存在则创建
    //int fd1 = open("log1.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);//设置起始权限
    //int fd2 = open("log2.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);//设置起始权限
    //int fd3 = open("log3.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);//设置起始权限
    //int fd4 = open("log4.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);//设置起始权限
    //int fd5 = open("log4.txt", O_WRONLY | O_CREAT | O_TRUNC,0666);//设置起始权限
    //printf("fd1:%d\n",fd1);
    //printf("fd2:%d\n",fd2);
    //printf("fd3:%d\n",fd3);
    //printf("fd4:%d\n",fd4);
    //printf("fd5:%d\n",fd5);
    //if(fd1 < 1)
    //{
    //    perror("open fail");
    //    exit(1);
    //}
    //const char* message = "aaaa";
    ////使用fd写入
    //write(fd1,message,strlen(message));

    return 0;
}




////00001
//#define ONE (1<<0)
////00010
//#define TWO (1<<1)
////00100
//#define THREE (1<<2)
////01000
//#define FOUR (1<<3)
////10000
//#define FIVE (1<<4)
//
//void PrintTest(int flags)
//{
//    if(flags & ONE)
//        printf("one\n");
//    if(flags & TWO)
//        printf("two\n");
//    if(flags & THREE)
//        printf("three\n");
//    if(flags & FOUR)
//        printf("four\n");
//    if(flags & FIVE)
//        printf("five\n");
//}
//
//int main()
//{
//    printf("--------------------------\n");
//    PrintTest(ONE);
//    printf("--------------------------\n");
//    PrintTest(TWO);
//    printf("--------------------------\n");
//    PrintTest(THREE);
//    printf("--------------------------\n");
//    PrintTest(ONE | TWO);
//    printf("--------------------------\n");
//    PrintTest(ONE | TWO | THREE ) ;
//    printf("--------------------------\n");
//    PrintTest(ONE | TWO | THREE | FOUR);
//    printf("--------------------------\n");
//




    //----------------------------------------------------
   // FILE* fp = fopen("test.txt","wb");
   // if(fp == NULL)
   // {
   //     perror("open fail");
   //     exit(1);
   // }
   // 
   // const char mess[] = "hello world\n";
   // int i = 0;
   // while(i < 10)
   // {
   //     fwrite(mess,sizeof(mess),1,fp);
   //     i++;
   // }

   // fclose(fp);
//    return 0;
//}
